<template>
	<view>
		<view class="lay-fs-c mg-md pd-md bk-white radius">
			<image class="radius" style="width: 200rpx; height: 200rpx;" :src="avatar"></image>
			<view class="summary mg-l-xxl">
				<view class="item mg-v-md">
					<view class="label">昵称：</view>
					<view class="head-title orange">{{nickname}}</view>
				</view>
				<view class="item">
					<view class="label">总重量：</view>
					<view>{{total.ret_weight}}</view>
				</view>
				<view class="item">
					<view class="label">总尾数：</view>
					<view>{{total.ret_num}}</view>
				</view>
				<view class="item">
					<view class="label">总积分：</view>
					<view>{{total.points}}</view>
				</view>
				<view class="item">
					<view class="label">总排名：</view>
					<view class="red text-bold text-xl">{{total.ranking}}</view>
				</view>
			</view>
		</view>
		
		<view class="mg-t-md lay-fs-c pd-h-sm">
			<view class="group-flg bk-green mg-sm mg-l--sm"></view>
			<text class="title">各场次成绩得分汇总</text>
		</view>
		
		<block v-for="(round, index) in rounds" :key="index">
			<view class="mg-lg">
				<!--
				<view class="lay-sb-c-c bk-white-gray pd-md">
					<text class="title green">{{round.name}}</text>
					<button v-if="round.deduct_points<0.01" class="mg-l-lg btn line-red red" @click="onDeductPoint(index+1, round.rows[0][0])">罚分</button>
					<button v-else class="mg-l-lg btn line-red red" @click="onCancelDeductPoint(index+1, round.rows[0][0])">取消罚分</button>
				</view>
				-->
				<MyTable :columns="columns" :rows="round.rows" :headerStyle="headerStyle" :bodyStyle="bodyStyle">
				</MyTable>
			</view>
		</block>
		
		<!-- 罚分模态窗口 -->
		<view class="cu-modal" :class="showModal?'show':''">
			<view class="cu-dialog">
				<view style="display: flex;justify-content: center; align-items: center; margin: 50rpx 0rpx; font-size: large; font-weight: bold;">
					<text>罚分</text>
				</view>
				<view>
					<view style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
						<view style="width:150upx">罚分：</view>
						<input style="width:400upx" type="number" v-model="deductPoints" placeholder="请输入罚分"></input>
					</view>
					
					<view style="display: flex; justify-content: space-around; align-items: center; margin: 50rpx 0rpx;">
						<button style="width: 200rpx; padding: 10rpx 0rpx; background-color: #E93323; color: white;" @click="hideModal">取消</button>
						<button style="width: 200rpx; padding: 10rpx 0rpx; background-color: #E93323; color: white;" @click="onOK">确定</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		raceDeductPoints
	} from '@/api/fsh.js';
	
	import MyTable from '@/components/my/MyTable.vue';
	import {MyTip} from '@/components/ssty/common.js';
	
	const app = getApp();
	export default {
		components: {
			MyTable
		},
		
		data() {
			return {
				nickname: '孟辉',
				avatar: "https://thirdwx.qlogo.cn/mmopen/vi_32/61wW7hT9648gqO9oTpSf2RSrsw07YzWgnGWSianfWUrd3WRw9xtHFeiciaxKfVscsdIia2cBHIwqib4nXOF5LyKMk7w/132",
				total: {
					ret_weight: '10斤',
					ret_num: 3,
					points: 1.5,
					ranking: 1,
				},
				
				columns: [
					{name: "分区", width: '130rpx'},
					{name: "钓位", width: '130rpx'},
					{name: "重量", width: '130rpx'},
					{name: "积分", width: '130rpx'},
					{name: "罚分", width: '130rpx'}
				],
				
				headerStyle: {
					'height': '80rpx', // $round-tab-h
					'background-color': '#FFF',
					'border-bottom': '1px solid #f8f8f8',
					'font-weight': 'bold'
				},
				bodyStyle: {
					'background-color': '#FFF',
				},
				
				rounds: [
					{
						name: '第1场',
						rows: [["1", "1", "2", "1", "--"]],
						deduct_points: 0
					},
					{
						name: '第2场',
						rows: [["2", "2", "3", "2", "2"]],
						deduct_points: 2
					}
				],
				
				showModal: false,
				deductPoints: ''
			}
		},
		onLoad(options) {
			this.userId = options.userId;
			let userScores = app.globalData.race_score.filter(rs=>rs.user_id==this.userId);
			let sumScore = userScores.filter(rs=>rs.round==0);
			console.log("userScores")
			console.log(userScores)
			console.log(sumScore)
			this.nickname = userScores[0].nickname;
			this.avatar = userScores[0].avatar;
			this.total.ret_weight = sumScore[0].ret_weight;
			this.total.ret_num = sumScore[0].ret_number;
			this.total.points = parseInt(sumScore[0].points);
			this.total.ranking = sumScore[0].ranking;
			
			let roundNumber = userScores.length;
			
			let rounds = [];
			for(let i = 1; i <= roundNumber; ++i) {
				let ary = userScores.filter(rs=>rs.round==i);
				if(ary.length>0)
				rounds.push({
					'name': `第${i}场`,
					'deduct_points': parseFloat(ary[0].deduct_points).toFixed(2),
					'rows': [[ary[0].race_partition, ary[0].seat_no, ary[0].ret_weight, ary[0].points, ary[0].deduct_points?ary[0].deduct_points:"--"]]
				});
			}
			this.rounds = rounds;
			
			let scoreTypeByNum = parseInt(userScores[0].score_type) == 1;
			let scoreTitle = scoreTypeByNum?"条数":"重量";
			this.columns[2].name = scoreTitle;
		},
		methods: {
			hideModal() {
				this.showModal = false;
			},
			onDeductPoint(roundNum, partitionNum) {
				this.showModal = true;
				this.deductPointRound = parseInt(roundNum);
				this.deductPointPartition = parseInt(partitionNum);
			},
			applyDeductPoint(round, points) {
				let userScores = app.globalData.race_score.filter(rs=>rs.user_id==this.userId);
				let ary = userScores.filter(rs=>rs.round==round);
				ary[0].deductPoints = points;
			},
			onOK() {
				let deductPoints = parseFloat(this.deductPoints).toFixed(2);
				if(!deductPoints) {
					MyTip("请输入罚分", 800);
					return;
				}
					
				uni.showLoading({
					title: '提交罚分中...',
					mask: true
				});
				
				raceDeductPoints(app.globalData.race.id, this.deductPointRound, this.deductPointPartition, this.userId, deductPoints).then(res => {
					uni.hideLoading();
					this.showModal = false;
					this.applyDeductPoint(this.deductPointRound, deductPoints);
					return this.$util.Tips({
						title: '提交罚分成功'
					}, {
						tab: 3,
						url: 1
					});
				}).catch(err => {
					uni.hideLoading();
					MyTip("提交罚分失败："+err);
				})
			},
			onCancelDeductPoint(roundNum, partitionNum) {
				this.deductPointRound = parseInt(roundNum);
				this.deductPointPartition = parseInt(partitionNum);
				
				uni.showLoading({
					title: '取消罚分中...',
					mask: true
				});
				raceDeductPoints(app.globalData.race.id, this.deductPointRound, this.deductPointPartition, 0).then(res => {
					uni.hideLoading();
					this.showModal = false;
					this.applyDeductPoint(this.deductPointRound, 0);
					return this.$util.Tips({
						title: '取消罚分成功'
					}, {
						tab: 3,
						url: 1
					});
				}).catch(err => {
					uni.hideLoading();
					MyTip("取消罚分失败："+err);
				});
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/leui.scss";
	page {
		background-color: $my-page-bkg-color;
	}
	
	.summary {
		> .item {
			@extend .lay-fs-c;
			>.label {
				@extend .title;
				width: 140rpx;
			}
		}
	}
	
	.mg-l--sm { margin-left: -$sm; }
</style>
